package name.jautzy.esi.server;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.labs.taskqueue.Queue;
import com.google.appengine.api.labs.taskqueue.QueueFactory;
import static com.google.appengine.api.labs.taskqueue.TaskOptions.Builder.*;

public class EveDbRemoveFromApplet extends HttpServlet {
	public static final long LIMIT_MILLIS = 1000 * 25; 
	
	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException {
		long startTime = System.currentTimeMillis();
		
		String kind = req.getParameter("kind");
		EdbDAO dao = new EdbDAO();
		
		Iterable<?> iter = null;
		if (kind.equals("Category"))
			iter = dao.getCategoryKeys();
		else if (kind.equals("Group"))
			iter = dao.getGroupKeys();
		else if (kind.equals("Equipment"))
			iter = dao.getEquipmentKeys();
		else if (kind.equals("Attribute"))
			iter = dao.getAttributeKeys();
		else if (kind.equals("Effect"))
			iter = dao.getEffectKeys();
		else if (kind.equals("Bonus"))
			iter = dao.getBonusKeys();
		
		for (Object o : iter) {
			dao.ofy().delete(o);
			
			if (System.currentTimeMillis() - startTime > LIMIT_MILLIS) {
				Queue queue = QueueFactory.getDefaultQueue();
				queue.add(url("/esi/evedbremoveapplet").param("kind", kind));
				break;
			}
		}
	}
}
